$(function () {
    var tag_table;
    layui.use('table',function () {
        var table = layui.table;
        tag_table = table.render({
            elem: '#tag_table' //指定原始表格元素选择器（推荐id选择器）
            ,height: 625 //容器高度
            ,cols: [[
                {fixed:'left',type:'checkbox'},
                {field: 'id', hide:true},
                {field: 'name',title:'标签名称'},
                {field: 'createTime',title:'添加时间'}
            ]] //设置表头
            ,toolbar:true
            ,page:true
            ,url:'/table/list/tags'
        });
    })
    $("#tag_insert_btn").click(function () {
        $("#tag_insert_btn").attr("disabled",true);
        var name = $("#name").val();
        if(tag_valid_name(name)){
            var params = $("#tag_insert_form").serialize();
            $.ajax({
                type:"post",
                url:"/tag/insert",
                data:params,
                success:function (r) {
                    layer.open({
                        icon:1,
                        title:"提示",
                        shadeClose:true,
                        content:r
                    });
                    $("#name").val("");
                    $("#tag_insert_btn").attr("disabled",false);
                    if(r=='success'){
                        tag_table = table_reload_insert(tag_table,"tag_table");
                    }else{
                        tag_table = table_reload(tag_table,"tag_table");
                    }
                }
            })
        }else{
            $("#tag_insert_btn").attr("disabled",false);
        }
    })
    $("#tag_del_btn").click(function () {
        layui.use('table',function () {
            var table = layui.table;
            var checkStatus = table.checkStatus('tag_table');
            var data = checkStatus.data;
            if(data.length>0){
                layer.confirm('真的删除麽？',function () {
                    $.ajax({
                        type:"post",
                        url:'/tag/delete',
                        dataType:"json",
                        data:JSON.stringify(data),
                        contentType: "application/json;charset=utf-8",
                        success:function (r) {
                            if(r.status='success'){
                                layer.msg("删除成功");
                            }else{
                                layer.msg("error");
                            }
                            tag_table = table_reload_del(tag_table,'tag_table');
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest.status);
                            console.log(XMLHttpRequest.readyState);
                            console.log(textStatus);
                        }
                    });
                })
            }else{
                layer.msg("请选中至少一条数据");
            }
        });
    })
})
function tag_valid_name(name) {
    if(isNull(name)||name.trim().length<1){
        layer.open({
            icon:0,
            title:"warning",
            shadeClose:true,
            content:"请输入标签名称"
        });
        return false;
    }
    if(!reg_valid_tag_name(name)){
        layer.open({
            icon:0,
            title:"warning",
            shadeClose:true,
            content:"标签名称格式1~20个字符"
        });
        return false;
    }
    return true;
}


